//
//  CustomTabBarViewController.m
//  CustomTabBar
//
//  Created by Peter Boctor on 1/2/11.
//
// Copyright (c) 2011 Peter Boctor
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//

#import "CustomTabBarViewController.h"

#import "TigerViewController.h"
#import "InformationViewController.h"
#import "CollectionViewController.h"
#import "MoreViewController.h"
#import "PInformationViewController.h"
#import "PCollectViewController.h"

#define SELECTED_VIEW_CONTROLLER_TAG 98456345

static NSArray* tabBarItems = nil;

@implementation CustomTabBarViewController
@synthesize tabBar;

- (void) awakeFromNib
{
    m_ButtonToFirst = [UIButton buttonWithType:UIButtonTypeCustom];
    [m_ButtonToFirst retain];
    m_ButtonToFirst.frame =CGRectMake(10, 8, 56, 30);
    [m_ButtonToFirst setImage:[UIImage imageNamed:@"home.png"] forState:UIControlStateNormal];
    [m_ButtonToFirst addTarget:self action:@selector(toTabBarFirst) forControlEvents:UIControlEventTouchUpInside];
	// Set up some fake view controllers each with a different background color so we can visually see the controllers getting swapped around
	
	TigerViewController *tigerViewController = [[TigerViewController alloc] init];
	UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:tigerViewController]; 
    [tigerViewController release];

//	InformationViewController *informationViewController = [[InformationViewController alloc] initWithNibName:@"InformationViewController" bundle:nil];
    PInformationViewController* iPInformationViewController = [[PInformationViewController alloc] init];
	UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:iPInformationViewController];
//    secondNavController.delegate =self;
    [iPInformationViewController release];

	
//	CollectionViewController *collectionViewController = [[CollectionViewController alloc] initWithNibName:@"CollectionViewController" bundle:nil];
    PCollectViewController * PCollect = [[PCollectViewController alloc] init];
	UINavigationController *thirdNavController = [[UINavigationController alloc] initWithRootViewController:PCollect]; 
//    thirdNavController.delegate = self;
//    [collectionViewController release];
    [PCollect release];
	
	MoreViewController *moreViewController = [[MoreViewController alloc] init];
	UINavigationController *fourthNavController = [[UINavigationController alloc] initWithRootViewController:moreViewController];
    fourthNavController.delegate = self;
    [fourthNavController.navigationBar addSubview:m_ButtonToFirst];
    [moreViewController release];

	tabBarItems = [[NSArray arrayWithObjects:
			  [NSDictionary dictionaryWithObjectsAndKeys:@"tab1.png", @"image", firstNavController, @"viewController", nil],
			  [NSDictionary dictionaryWithObjectsAndKeys:@"tab2.png", @"image", secondNavController, @"viewController", nil],
			  [NSDictionary dictionaryWithObjectsAndKeys:@"tab3.png", @"image", thirdNavController, @"viewController", nil],
			  [NSDictionary dictionaryWithObjectsAndKeys:@"tab4.png", @"image", fourthNavController, @"viewController", nil], nil] retain];
    
    [fourthNavController release];
    [thirdNavController release];
    [secondNavController release];
    [firstNavController release];

}

- (void)viewDidLoad
{
	[super viewDidLoad];
  
	// Create a custom tab bar passing in the number of items, the size of each item and setting ourself as the delegate
	self.tabBar = [[[CustomTabBar alloc] initWithItemCount:tabBarItems.count itemSize:CGSizeMake(self.view.frame.size.width/tabBarItems.count, 46) tag:0 delegate:self] autorelease];
  
	// Place the tab bar at the bottom of our view
	tabBar.frame = CGRectMake(0,self.view.frame.size.height-46,self.view.frame.size.width, 92);
	[self.view addSubview:tabBar];
  
	// Select the first tab
	[tabBar selectItemAtIndex:0];
	[self touchDownAtItemAtIndex:0];
}

#pragma mark -
#pragma mark CustomTabBarDelegate

- (UIImage*) imageFor:(CustomTabBar*)tabBar atIndex:(NSUInteger)itemIndex
{
  // Get the right data
  NSDictionary* data = [tabBarItems objectAtIndex:itemIndex];
  // Return the image for this tab bar item
  return [UIImage imageNamed:[data objectForKey:@"image"]];
}

- (UIImage*) backgroundImage
{
  // The tab bar's width is the same as our width
  CGFloat width = self.view.frame.size.width;
  // Get the image that will form the top of the background
  UIImage* topImage = [UIImage imageNamed:@"bottom_bg.png"];
  
  // Create a new image context
  UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, topImage.size.height*2), NO, 0.0);
  
  // Create a stretchable image for the top of the background and draw it
  UIImage* stretchedTopImage = [topImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
  [stretchedTopImage drawInRect:CGRectMake(0, 0, width, topImage.size.height)];
  
  // Draw a solid black color for the bottom of the background
  [[UIColor blackColor] set];
  CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, topImage.size.height, width, topImage.size.height));
  
  // Generate a new image
  UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return resultImage;
}

// This is the blue background shown for selected tab bar items
- (UIImage*) selectedItemBackgroundImage
{
  return [UIImage imageNamed:@"TabBarItemSelectedBackground.png"];
}

// This is the glow image shown at the bottom of a tab bar to indicate there are new items
- (UIImage*) glowImage
{
  UIImage* tabBarGlow = [UIImage imageNamed:@"tab_glow.png"];
  
  // Create a new image using the TabBarGlow image but offset 4 pixels down
  UIGraphicsBeginImageContextWithOptions(CGSizeMake(tabBarGlow.size.width, tabBarGlow.size.height), NO, 0.0);

  // Draw the image
  [tabBarGlow drawAtPoint:CGPointZero];

  // Generate a new image
  UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return resultImage;
}

// This is the embossed-like image shown around a selected tab bar item
- (UIImage*) selectedItemImage
{
  // Use the TabBarGradient image to figure out the tab bar's height (22x2=44)
  UIImage* tabBarGradient = [UIImage imageNamed:@"TabBarGradient.png"];
  CGSize tabBarItemSize = CGSizeMake(self.view.frame.size.width/tabBarItems.count, tabBarGradient.size.height*2);
  UIGraphicsBeginImageContextWithOptions(tabBarItemSize, NO, 0.0);

  // Create a stretchable image using the TabBarSelection image but offset 4 pixels down
  [[[UIImage imageNamed:@"TabBarSelection.png"] stretchableImageWithLeftCapWidth:4.0 topCapHeight:0] drawInRect:CGRectMake(0, 4.0, tabBarItemSize.width, tabBarItemSize.height-4.0)];  

  // Generate a new image
  UIImage* selectedItemImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  
  return selectedItemImage;
}

- (UIImage*) tabBarArrowImage
{
  return [UIImage imageNamed:@"TabBarGradient.png"];

}

- (void) touchDownAtItemAtIndex:(NSUInteger)itemIndex
{
  // Remove the current view controller's view
  UIView* currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG];
  [currentView removeFromSuperview];
  
  // Get the right view controller
  NSDictionary* data = [tabBarItems objectAtIndex:itemIndex];
  UIViewController* viewController = [data objectForKey:@"viewController"];

  // Use the TabBarGradient image to figure out the tab bar's height (22x2=44)
  UIImage* tabBarGradient = [UIImage imageNamed:@"TabBarGradient.png"];

  // Set the view controller's frame to account for the tab bar
  viewController.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height-(tabBarGradient.size.height*2));

  // Se the tag so we can find it later
  viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;
  
  // Add the new view controller's view
  [self.view insertSubview:viewController.view belowSubview:tabBar];
  
  // In 1 second glow the selected tab
  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(addGlowTimerFireMethod:) userInfo:[NSNumber numberWithInteger:itemIndex] repeats:NO];
    
    if (itemIndex==2) {
//        NSDictionary *dict = [tabBarItems objectAtIndex:2];
//        UINavigationController *navController = (UINavigationController *)[dict objectForKey:@"viewController"];
//        CollectionViewController *collectionViewController = [navController.viewControllers objectAtIndex:0];
//        [collectionViewController reloadData];
    }
  
}

- (void)addGlowTimerFireMethod:(NSTimer*)theTimer
{
  // Remove the glow from all tab bar items
  for (NSUInteger i = 0 ; i < tabBarItems.count ; i++)
  {
    [tabBar removeGlowAtIndex:i];
  }
  
  // Then add it to this tab bar item
  [tabBar glowItemAtIndex:[[theTimer userInfo] integerValue]];
}

#pragma -
#pragma UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if ([viewController isKindOfClass:[MoreViewController class]]||
        [viewController isKindOfClass:[CollectionViewController class]]||
        [viewController isKindOfClass:[InformationViewController class]]) {
        [m_ButtonToFirst removeFromSuperview];
        [navigationController.navigationBar addSubview:m_ButtonToFirst];
    }else{
        [m_ButtonToFirst removeFromSuperview];
    }
}

-(void) toTabBarFirst{
    [tabBar selectItemAtIndex:0];
	[self touchDownAtItemAtIndex:0];
}

- (void)dealloc
{
    [m_ButtonToFirst release];
  [super dealloc];
  [tabBar release];
}

@end
